package com.codeman.bytebuddy;

import com.alibaba.fastjson.JSONArray;
import com.codeman.bytebuddy.component.ByteBuddyService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: JCccc
 * @Description:
 * @Date: 1/1/1 1:11
 */
@RestController
public class TestController {

    @Resource
    private ByteBuddyService byteBuddyService;

    /**
     * excel导入
     *
     * @return
     */
    @PostMapping(value = "/importTest")
    public void importTest( @RequestParam("file") MultipartFile file) throws IOException {
        String name2 = "com.codeman.bytebuddy.virtual.ProjectGroupExcelVO";
        Class<?> class2 = ByteBuddyService.VIRTUAL_CLASS_MAP.get(name2);
//        List<ProjectGroupExcelVO> projectGroupExcelVOList = MyExcelUtils.importExcel(file, 1, 2, ProjectGroupExcelVO.class);
        List<?> projectGroupExcelVOList = MyExcelUtils.importExcel(file, 1, 2, class2);

        System.out.println(projectGroupExcelVOList.toString());

        System.out.println("-----------------------------------");

        System.out.println("写入数据库");
    }



    /**
     * excel导出 
     *
     * @return
     */
    @GetMapping(value = "/exportTest")
    public void export(HttpServletResponse response) {

        String name1 = "com.codeman.bytebuddy.virtual.GroupUserExcelVO";
        String name2 = "com.codeman.bytebuddy.virtual.ProjectGroupExcelVO";
        Class<?> class1 = ByteBuddyService.VIRTUAL_CLASS_MAP.get(name1);
        Class<?> class2 = ByteBuddyService.VIRTUAL_CLASS_MAP.get(name2);

        String dataJson = "[{\"groupName\":\"小组A\",\"groupSlogan\":\"天天向上,爱学习！\",\"groupType\":\"奋斗类型\",\"groupUsers\":[{\"age\":18,\"name\":\"小收\",\"phone\":\"123456\"},{\"age\":20,\"name\":\"小藏\",\"phone\":\"654321\"}]},{\"groupName\":\"小组B\",\"groupSlogan\":\"跟着JC学java，稳！\",\"groupType\":\"努力类型\",\"groupUsers\":[{\"age\":12,\"name\":\"小点\",\"phone\":\"123456\"},{\"age\":15,\"name\":\"小赞\",\"phone\":\"654321\"},{\"age\":10000,\"name\":\"JCccc\",\"phone\":\"136919xxxxx\"}]}]\n";

        List<?> projectGroupList = JSONArray.parseArray(dataJson, class2);


        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));
       
        MyExcelUtils.exportExcel(projectGroupList,
                "小组信息",
                "小组信息",
                ProjectGroupExcelVO.class,
                "小组信息文件"+time+".xls",response);



    }

    public static void main(String[] args) {
        List<ProjectGroupExcelVO> projectGroupList=new ArrayList<>();

        //小组A数据模拟
        ProjectGroupExcelVO groupA=new ProjectGroupExcelVO();
        groupA.setGroupName("小组A");
        groupA.setGroupSlogan("天天向上,爱学习！");
        groupA.setGroupType("奋斗类型");
        List<GroupUserExcelVO> groupUserAList=new ArrayList<>();
        GroupUserExcelVO groupUser1=new GroupUserExcelVO();
        groupUser1.setName("小收");
        groupUser1.setPhone("123456");
        groupUser1.setAge(18);

        GroupUserExcelVO groupUser2=new GroupUserExcelVO();
        groupUser2.setName("小藏");
        groupUser2.setPhone("654321");
        groupUser2.setAge(20);
        groupUserAList.add(groupUser1);
        groupUserAList.add(groupUser2);
        groupA.setGroupUsers(groupUserAList);


        //小组B数据模拟
        ProjectGroupExcelVO groupB=new ProjectGroupExcelVO();
        groupB.setGroupName("小组B");
        groupB.setGroupSlogan("跟着JC学java，稳！");
        groupB.setGroupType("努力类型");
        List<GroupUserExcelVO> groupBUserBList=new ArrayList<>();
        GroupUserExcelVO groupUserB1=new GroupUserExcelVO();
        groupUserB1.setName("小点");
        groupUserB1.setPhone("123456");
        groupUserB1.setAge(12);

        GroupUserExcelVO groupUserB2=new GroupUserExcelVO();
        groupUserB2.setName("小赞");
        groupUserB2.setPhone("654321");
        groupUserB2.setAge(15);

        GroupUserExcelVO groupUserB3=new GroupUserExcelVO();
        groupUserB3.setName("JCccc");
        groupUserB3.setPhone("136919xxxxx");
        groupUserB3.setAge(10000);

        groupBUserBList.add(groupUserB1);
        groupBUserBList.add(groupUserB2);
        groupBUserBList.add(groupUserB3);
        groupB.setGroupUsers(groupBUserBList);

        projectGroupList.add(groupA);
        projectGroupList.add(groupB);
        System.out.println(JSONArray.toJSONString(projectGroupList));
    }


    /**
     * excel导出
     *
     * @return
     */
    @GetMapping(value = "/exportTest0")
    public void export0(HttpServletResponse response) {

        List<ProjectGroupExcelVO> projectGroupList=new ArrayList<>();

        //小组A数据模拟
        ProjectGroupExcelVO groupA=new ProjectGroupExcelVO();
        groupA.setGroupName("小组A");
        groupA.setGroupSlogan("天天向上,爱学习！");
        groupA.setGroupType("奋斗类型");
        List<GroupUserExcelVO> groupUserAList=new ArrayList<>();
        GroupUserExcelVO groupUser1=new GroupUserExcelVO();
        groupUser1.setName("小收");
        groupUser1.setPhone("123456");
        groupUser1.setAge(18);

        GroupUserExcelVO groupUser2=new GroupUserExcelVO();
        groupUser2.setName("小藏");
        groupUser2.setPhone("654321");
        groupUser2.setAge(20);
        groupUserAList.add(groupUser1);
        groupUserAList.add(groupUser2);
        groupA.setGroupUsers(groupUserAList);


        //小组B数据模拟
        ProjectGroupExcelVO groupB=new ProjectGroupExcelVO();
        groupB.setGroupName("小组B");
        groupB.setGroupSlogan("跟着JC学java，稳！");
        groupB.setGroupType("努力类型");
        List<GroupUserExcelVO> groupBUserBList=new ArrayList<>();
        GroupUserExcelVO groupUserB1=new GroupUserExcelVO();
        groupUserB1.setName("小点");
        groupUserB1.setPhone("123456");
        groupUserB1.setAge(12);

        GroupUserExcelVO groupUserB2=new GroupUserExcelVO();
        groupUserB2.setName("小赞");
        groupUserB2.setPhone("654321");
        groupUserB2.setAge(15);

        GroupUserExcelVO groupUserB3=new GroupUserExcelVO();
        groupUserB3.setName("JCccc");
        groupUserB3.setPhone("136919xxxxx");
        groupUserB3.setAge(10000);

        groupBUserBList.add(groupUserB1);
        groupBUserBList.add(groupUserB2);
        groupBUserBList.add(groupUserB3);
        groupB.setGroupUsers(groupBUserBList);

        projectGroupList.add(groupA);
        projectGroupList.add(groupB);


        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm_ss"));

        MyExcelUtils.exportExcel(projectGroupList,
                "小组信息",
                "小组信息",
                ProjectGroupExcelVO.class,
                "小组信息文件"+time+".xls",response);



    }

}
